﻿using System;
using RazakiBLL;
using RazakiBLL.Managers;
using RazakiBLL.Entities;
using RazakiSite.Classes;

namespace RazakiSite.PagesUser
{
    public partial class UserDetailEdit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.SetDefaultFormControls(BtnSend.UniqueID, TxtName.ClientID);

            if (IsPostBack)
                return;
            ConfigControls();
        }

        protected override void ConfigControls()
        {
            DrpDLSex.InsertDefaultItem();
        }

        protected void BtnSend_Click(object sender, EventArgs e)
        {
            //<validation
            string message;
            if (!ValidateControls(out message))
            {
                UCInfo.Text = message;
                UCInfo.InfoType = EInfoType.error;
                UCInfo.Hide = false;

                return;
            }
            //>

            string firstName = TxtName.Text;
            string lastName = TxtLastName.Text;
            bool sex = bool.Parse(DrpDLSex.SelectedValue);
            byte[] image = uploadFile.GetFile();
            UowRazaki uowRazaki = new UowRazaki();

            UserDetail userDetail = new UserDetail
                                        {
                                            UserId = UtilitiesUI.GetCurrentUserId(),
                                            FirstName = firstName,
                                            LastName = lastName,
                                            Sex = sex,
                                            Picture = image,
                                            //Todo: برای دو گزینه پایین فیلد ورودی در فرم ساخته شود تا خالی نباشند
                                            Cellphone = string.Empty,
                                            Telephone = string.Empty,
                                            LastUpdatedDate = DateTime.Now
                                        };

            uowRazaki.UserDetailRepository.Add(userDetail);
            if (uowRazaki.Save() > 0)
            {
                UCInfo.Text = UtilitiesUI.Sentences(ESentences.success);
                UCInfo.InfoType = EInfoType.success;
            }
            else
            {
                UCInfo.Text = UtilitiesUI.Sentences(ESentences.error);
                UCInfo.InfoType = EInfoType.error;
            }

            UCInfo.Hide = false;
            uploadFile.ClearFile();
        }

        /// <summary>
        /// در صورتی که اعتبارسنجی منفی باشد. مقدار پیام بازگشتی پر می باشد.
        /// </summary>
        /// <returns></returns>
        private bool ValidateControls(out string message)
        {
            string firstName = TxtName.Text;
            string lastName = TxtLastName.Text;

            message = string.Empty;

            if (string.IsNullOrWhiteSpace(firstName))
            { message = string.Format(UtilitiesUI.Sentences(ESentences.requiredFill), "نام"); return false; }

            if (string.IsNullOrWhiteSpace(lastName))
            { message = string.Format(UtilitiesUI.Sentences(ESentences.requiredFill), "نام خانوادگی"); return false; }

            if (DrpDLSex.SelectedValue == UtilitiesBLL._DECLINE_ID.ToString())
            { message = string.Format(UtilitiesUI.Sentences(ESentences.requiredSelect), "جنسیت"); return false; }

            if (!uploadFile.FileIsCorrect)
            { message = string.Format(UtilitiesUI.Sentences(ESentences.incorect), "تصویر"); return false; }

            return true;
        }
    }
}